package com.haixiaoke.saas.enterprise.dept.controller;

import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.dept.domain.Dept;
import com.haixiaoke.saas.dept.service.IDeptService;
import com.haixiaoke.saas.dept.domain.DeptTree;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.system.framework.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 企业端部门管理Controller
 *
 * @author panda
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/enterprise/dept/dept")
public class EnterpriseDeptController extends BaseController {
    @Autowired
    private IDeptService bizDeptService;

    @GetMapping("/list")
    public AjaxResult list(Dept dept) {
        dept.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        List<Dept> list = bizDeptService.selectDeptList(dept);
        return success(list);
    }

    @PreAuthorize("@ss.hasPermi('enterprise:dept:list')")
    @GetMapping("/tree/list")
    public AjaxResult treeList(Dept dept) {
        dept.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        List<DeptTree> list = bizDeptService.buildTreeList(dept);
        return success(list);
    }

    @GetMapping("/tree/addList")
    public AjaxResult addList(Dept dept) {
        dept.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        List<DeptTree> list = bizDeptService.buildTreeList(dept);
        DeptTree node = new DeptTree();
        node.setId("0");
        node.setName("无上级");
        list.add(0, node);
        return success(list);
    }

    /**
     * 导出企业端部门管理列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:dept:export')")
    @Log(title = "企业端部门管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Dept dept) {
        List<Dept> list = bizDeptService.selectDeptList(dept);
        ExcelUtil<Dept> util = new ExcelUtil<Dept>(Dept.class);
        util.exportExcel(response, list, "企业端部门管理数据");
    }

    /**
     * 获取企业端部门管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:dept:query')")
    @GetMapping(value = "/{deptId}")
    public AjaxResult getInfo(@PathVariable("deptId") Long deptId) {
        return success(bizDeptService.selectDeptByDeptId(deptId));
    }

    /**
     * 新增企业端部门管理
     */
    @PreAuthorize("@ss.hasPermi('enterprise:dept:add')")
    @Log(title = "企业端部门管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dept dept) {
        dept.setCreateBy(getUsername());
        dept.setTenantId(SecurityUtils.getLoginUser().getTenantId());
        return toAjax(bizDeptService.insertDept(dept));
    }

    /**
     * 修改企业端部门管理
     */
    @PreAuthorize("@ss.hasPermi('enterprise:dept:edit')")
    @Log(title = "企业端部门管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dept dept) {
        dept.setUpdateBy(getUsername());
        return toAjax(bizDeptService.updateDept(dept));
    }

    /**
     * 删除企业端部门管理
     */
    @PreAuthorize("@ss.hasPermi('enterprise:dept:remove')")
    @Log(title = "企业端部门管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deptId}")
    public AjaxResult remove(@PathVariable("deptId") Long deptId) {
        if (bizDeptService.hasChildByDeptId(deptId)) {
            return warn("部门已创建下级部门，不能删除");
        }
        if (bizDeptService.checkDeptExistUser(deptId)) {
            return warn("部门已关联企业用户，不能删除");
        }
        return toAjax(bizDeptService.deleteDeptByDeptId(deptId));
    }
}
